<template>
  <div style="min-width: 809px">
    <div v-if="$route.path == '/storage'">
      <div class="vs-search-box">
        <el-form ref="form" :inline="true" :model="paging" label-width="120px">
          <el-form-item label="商品名称：">
            <el-input style="width: 250px" v-model="paging.title"></el-input>
          </el-form-item>
          <el-form-item label="入库时间：">
            <el-date-picker
              v-model="paging.time"
              type="date"
              value-format="timestamp"
              placeholder="选择日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="" class="vs-right-btns">
            <el-button @click="reset">重置</el-button>
            <el-button @click="handleCurrentChange(1)" type="primary"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="vs-table-box">
        <div class="operation-box clear-float">
          <!-- <div class="title-left"> 分销列表 </div> -->
          <el-button
            @click="utils.goUrl('/storageInfo?type=2')"
            class="left-auto"
            type="primary"
            >新增</el-button
          >
        </div>
        <el-table
          v-loading="loading"
          :data="tableData"
          header-row-class-name="has-gutter-table"
          style="width: 100%"
        >
          <el-table-column prop="goods_id" label="商品ID"> </el-table-column>
          <el-table-column prop="goods.cate.title" label="商品分类">
          </el-table-column>
          <el-table-column prop="goods.title" label="商品名称">
          </el-table-column>
          <el-table-column
            prop="goods.sku.sku_text"
            label="商品规格"
            min-width="140"
          >
            <template slot-scope="scope">
              {{ scope.row.goods.sku.sku_text }}
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="入库类型" width="90">
            <template slot-scope="scope">
              {{ scope.row.type | beputType }}
            </template>
          </el-table-column>
          <el-table-column
            prop="goods.sku.waringSku.stock"
            label="入库数量（件）"
            width="80"
          >
            <template slot-scope="scope" v-if="scope.row.wareSku">
              {{ scope.row.wareSku[0].stock }}
            </template>
          </el-table-column>
          <el-table-column label="入库单价（元）" width="80">
            <template slot-scope="scope">
              {{ scope.row.goods.sku.sale_price }}
            </template>
          </el-table-column>
          <el-table-column prop="create_time" width="80" label="库存数量（件）">
            <template slot-scope="scope">
              {{ scope.row.goods.sku.stock }}
            </template>
          </el-table-column>
          <el-table-column prop="source" label="商品来源"> </el-table-column>
          <el-table-column prop="create_time" min-width="160" label="入库时间">
          </el-table-column>
          <el-table-column prop="remarks" label="备注" min-width="130">
            <template slot-scope="scope">
              <span class="line-2">{{ scope.row.remarks }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="create_time" label="调动时间" min-width="130">
            <template slot-scope="scope">
              <span class="line-2">{{ scope.row.create_time }}</span>
            </template>
          </el-table-column> -->
          <el-table-column fixed="right" width="120" label="操作">
            <template slot-scope="scope">
              <span @click="setRow(1, scope.row)" class="tab-table-text"
                >查看</span
              >
              <span @click="setRow(3, scope.row)" class="tab-table-text"
                >编辑</span
              >
              <!-- <span @click="setRow(3,scope.row)" class="tab-table-text">修改</span>
                            <span @click="setRow(2,scope.row)" class="tab-lv-text">上架</span> -->
              <!-- <span @click="setRow(2,scope.row)" class="tab-red-text">删除</span> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="vs-paging-box">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="paging.page"
            :page-size="paging.limit"
            layout="sizes,prev, pager, next, jumper"
            :total="paging.total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <router-view></router-view>
    <el-dialog title="分销记录" :visible.sync="isInfo">
      <!-- width="570px" -->
      <el-table
        :data="recordList"
        v-loading="loading2"
        header-row-class-name="has-gutter-table"
        style="width: 100%"
      >
        <el-table-column prop="company.title" label="公司名称">
        </el-table-column>
        <el-table-column prop="create_time" label="邀请时间"> </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="isInfo = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      recordList: [],
      loading: false,
      loading2: false,
      isBtn: false,
      isInfo: false,
      dialogVisible: false,
      paging: {
        title: "",
        time: "",
        page: 1,
        total: 0,
        limit: 10,
      },

      form: {
        date: "",
      },
      tableData: [],
    };
  },
  created() {
    if (this.$route.path == "/storage") {
      this.handleCurrentChange(1);
    }
  },
  filters: {
    beputType(val) {
      switch (val) {
        case 1:
          val = "采购入库";
          break;
        case 2:
          val = "调拨入库";
          break;
        case 3:
          val = "赠品入库";
          break;
      }
      return val;
    },
  },
  methods: {
    async setRow(type, row) {
      if (type == 2) {
        this.$msgbox({
          title: "提示",
          message: "确定要删除该入库商品吗?",
          type: "warning",
          showCancelButton: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          beforeClose: async (action, instance, done) => {
            if (action === "confirm") {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = "执行中...";
              let { code, data } = await this.$Http.delWarehousing({
                id: String(row.id),
              });
              if (code) {
                this.$message.success("删除成功!");
                this.handleCurrentChange(1);
              }
              done();
              instance.confirmButtonLoading = false;
            } else {
              done();
            }
          },
        })
          .then((action) => {})
          .catch(() => {});
      } else {
        this.utils.goUrl(`/storageInfo?type=${type}&id=${row.id}`);
      }
      return;
      if (type == 1) {
        this.dialogVisible = true;
        setTimeout(() => {
          this.ruleForm = Object.assign({}, row);
        }, 200);
      } else if (type == 2) {
        this.isInfo = true;
        this.loading2 = true;
        let { code, data } = await this.$Http.catdistribution({
          id: row.id,
        });
        this.loading2 = false;
        if (code) {
          this.recordList = data;
        } else this.recordList = [];
      }
    },
    handleClose(dom) {
      this.$refs["ruleForm"].resetFields();
      this.dialogVisible = false;
      dom && dom();
    },
    async repairGetList() {
      this.loading = true;
      let obj = Object.assign({}, this.paging);
      obj.time /= 1000;
      let { code, data } = await this.$Http.getWarehousing(obj);
      this.loading = false;
      if (code) {
        // let list = [];
        // data.data.forEach(v=>{
        //     list.push(...v.goods.sku.map(v2=>{
        //         v.goods.sku = v2;
        //         return v;
        //     }))
        // });
        // console.log(list,"list");
        this.tableData = data.data.map((v) => {
          if (v.goods.sku.length) {
            v.goods.sku = v.goods.sku[0];
          }
          return v;
        });
        this.paging.total = data.total;
      }
    },
    reset() {
      //重置
      this.paging = this.$options.data().paging;
      this.form.date = "";
      this.handleCurrentChange(1);
    },
    handleSizeChange(val) {
      this.paging.limit = val;
      this.repairGetList();
    },
    handleCurrentChange(val) {
      this.paging.page = val;
      this.repairGetList();
    },
  },
};
</script>

<style lang="scss" scoped>
.image-thumbnail {
  width: 130px;
  height: 130px;
}
</style>